package com.one.blocks.rbac.controller;

import com.one.blocks.mvc.controller.AbstractController;
import com.one.blocks.mvc.controller.valid.Custom;
import com.one.blocks.mvc.controller.valid.Update;
import com.one.blocks.mvc.request.PageRequest;
import com.one.blocks.mvc.response.ResponseHelper;
import com.one.blocks.mvc.response.WebPageResponse;
import com.one.blocks.mvc.response.WebResponse;
import com.one.blocks.mvc.util.TreeUtil;
import com.one.blocks.rbac.controller.convert.SysOrgConvert;
import com.one.blocks.rbac.controller.convert.SysRoleOrgConvert;
import com.one.blocks.rbac.controller.query.SysOrgQuery;
import com.one.blocks.rbac.controller.valid.NoAllocatedData;
import com.one.blocks.rbac.controller.vo.SysOrgVo;
import com.one.blocks.rbac.controller.vo.SysRoleGrantOrgVo;
import com.one.blocks.rbac.controller.vo.tree.SysOrgTreeNodeVo;
import com.one.blocks.rbac.domain.SysOrg;
import com.one.blocks.rbac.domain.SysRole;
import com.one.blocks.rbac.enums.RoleTypeEnum;
import com.one.blocks.rbac.manager.SysOrgManager;
import com.one.blocks.rbac.manager.SysRoleManager;
import com.one.blocks.rbac.manager.SysRoleOrgManager;
import com.one.blocks.rbac.util.SubjectRoleHelper;
import com.one.blocks.security.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:idler41@163.con">idler41</a> created on 2022-05-01 10:06:14
 */
@Api(tags = "系统组织接口")
@RestController
@RequestMapping("/system/org")
@Slf4j
@Validated
public class SysOrgController extends AbstractController<SysOrgManager, SysOrgConvert, SysOrg, SysOrgVo, SysOrgQuery> {

    @Autowired
    private SysRoleManager sysRoleManager;

    @Autowired
    private SysRoleOrgManager sysRoleOrgManager;

    @Autowired
    public SysOrgController(SysOrgManager manager, SysOrgConvert sysOrgConvert) {
        super(manager, sysOrgConvert);
    }

    @ApiOperation("查询单个组织")
    @PreAuthorize("system:org:view")
    @GetMapping
    public WebResponse<SysOrgVo> get(@Validated @NotNull @RequestParam Integer id) {
        return super.generalGet(id);
    }

    @ApiOperation("根据id批量查询组织")
    @PreAuthorize("system:org:view")
    @GetMapping("/batch")
    public WebResponse<List<SysOrgVo>> getIds(@Validated @NotNull @Size(min = 1, max = 100) @RequestParam("ids[]") Set<Integer> ids) {
        return super.generalGetIds(ids);
    }

    @ApiOperation("分页查询组织")
    @PreAuthorize("system:org:view")
    @GetMapping("/page")
    public WebPageResponse<SysOrgVo> page(@Validated PageRequest request, @Validated SysOrgQuery query) {
        return super.generalPage(request, query);
    }

    @ApiOperation("添加一个组织")
    @PreAuthorize("system:org:insert")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Serializable> insert(@Validated @RequestBody SysOrgVo sysOrgVo) {
        return super.generalInsert(sysOrgVo);
    }

    @ApiOperation("更新一个组织(更新所有字段)")
    @PreAuthorize("system:org:update")
    @PutMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Boolean> update(@Validated({Update.class, Custom.class}) @RequestBody SysOrgVo sysOrgVo) {
        return super.generalUpdate(sysOrgVo);
    }

    @ApiOperation("更新一个组织(只更新非空字段)")
    @PreAuthorize("system:org:update")
    @PatchMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Boolean> updateSelective(@Validated @RequestBody SysOrgVo sysOrgVo) {
        return super.generalUpdateSelective(sysOrgVo);
    }

    @ApiOperation("删除组织")
    @PreAuthorize("system:org:delete")
    @DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public WebResponse<Integer> delete(@Validated @NotNull @Size(min = 1, max = 100) @NoAllocatedData(manager = SysRoleOrgManager.class) @RequestBody Set<Integer> ids) {
        return super.generalDeleteByIds(ids);
    }

    @ApiOperation("查询我的组织树")
    @PreAuthorize("system:org:view")
    @GetMapping("/tree")
    public WebResponse<List<SysOrgTreeNodeVo>> tree() {
        return ResponseHelper.success(TreeUtil.buildTree(convert.toTreeNodeList(manager.selectByRole(SubjectRoleHelper.getSysRole()))));
    }

    @ApiOperation("查询指定角色的组织树")
    @PreAuthorize("system:org:view")
    @GetMapping("/tree/role")
    public WebResponse<List<SysOrgTreeNodeVo>> treeOfRole(@NotNull Integer roleId) {
        SysRole sysRole = sysRoleManager.get(roleId);
        if (!RoleTypeEnum.COMMON.match(sysRole.getType())) {
            return ResponseHelper.success(null);
        }
        return ResponseHelper.success(TreeUtil.buildTree(convert.toTreeNodeList(manager.selectByRole(sysRole))));
    }

    @ApiOperation("赋予指定角色目录权限")
    @PreAuthorize("system:org:grant")
    @PostMapping("/grant")
    public WebResponse<Boolean> grant(@Valid @RequestBody SysRoleGrantOrgVo sysRoleGrantVo) {
        SysRole sysRole = sysRoleManager.get(sysRoleGrantVo.getRoleId());
        if (!RoleTypeEnum.COMMON.match(sysRole.getType())) {
            return ResponseHelper.success(false);
        }
        return ResponseHelper.success(sysRoleOrgManager.grant(sysRole.getId(), SysRoleOrgConvert.INSTANCE.grantVoToEntity(sysRoleGrantVo)));
    }

}